//============================================================================================
/**
 * @file	legend_demo_call.c
 * @brief	伝説デモイベント処理
 * @author	Hiroyuki Nakamura
 * @date	09.01.28
 */
//============================================================================================
#include "common.h"

#include "system/wipe.h"

#include "../../field/fieldsys.h"
#include "../../field/field_event.h"
#include "../../field/ev_mapchange.h"
#include "../../field/map_tool.h"
#include "../../field/zonedata.h"
#include "../../field/fieldmap.h"
#include "../../field/fieldmap_work.h"
#include "../../field/fld_bgm.h"
#include "../../field/ev_time.h"

#include "demo/legend_demo.h"

#include "legend_demo_call.h"


//============================================================================================
//	定数定義
//============================================================================================

// 呼び出しワーク
typedef struct {
	GLST_DATA	glst;
	u32	mode;	// モード
	u32	time;	// 時間帯
	u32	seq;	// シーケンス
}LD_CALL_WORK;

// イベントワーク
typedef struct {
	LD_CALL_WORK * call;	// 呼び出しワーク
	void * dat;				// デモデータ
	u32	seq;				// シーケンス
}LD_EVENT_WORK;


//============================================================================================
//	プロトタイプ宣言
//============================================================================================
static BOOL LegendDemoMain( GMEVENT_CONTROL * event );
static void EventSet( FIELDSYS_WORK * fsys, LD_CALL_WORK * cwk );
static BOOL LegendDemoEvent( GMEVENT_CONTROL * event );


//--------------------------------------------------------------------------------------------
/**
 * 伝説デモアプリ呼び出し
 *
 * @param	fsys	フィールドシステム
 * @param	mode	呼び出しモード ( 0 = ホウオウ, 1 = ルギア, 2 = アルセウス )
 *
 * @return	none
 */
//--------------------------------------------------------------------------------------------
void LEGENDDEMOCALL_EventCall( FIELDSYS_WORK * fsys, u32 mode )
{
	LD_CALL_WORK * wk;

	wk = sys_AllocMemoryLo( HEAPID_WORLD, sizeof(LD_CALL_WORK) );

	wk->glst = *(fsys->glst_data);
	wk->mode = mode;
	wk->seq  = 0;

	switch( EVTIME_GetTimeZone(fsys) ){
	case TIMEZONE_MORNING:	// 04:00 - 09:59
	case TIMEZONE_NOON:		// 10:00 - 16:59
		wk->time = LEGENDDEMO_TIME_MORNING;		// 朝
		break;

	case TIMEZONE_EVENING:	// 17:00 - 19:59
		wk->time = LEGENDDEMO_TIME_AFTERNOON;	// 昼
		break;

	case TIMEZONE_NIGHT:	// 20:00 - 23:59
	case TIMEZONE_MIDNIGHT:	// 00:00 - 03:59
		wk->time = LEGENDDEMO_TIME_EVENING;		// 夜
		break;
	default:
		GF_ASSERT(0);
		wk->time = LEGENDDEMO_TIME_AFTERNOON;	// 昼
	}

	FieldEvent_Call( fsys->event, LegendDemoMain, wk );
}

//--------------------------------------------------------------------------------------------
/**
 * 呼び出しイベント
 *
 * @param	event
 *
 * @return	処理状況
 */
//--------------------------------------------------------------------------------------------
static BOOL LegendDemoMain( GMEVENT_CONTROL * event )
{
	FIELDSYS_WORK * fsys;
	LD_CALL_WORK * wk;

	fsys = FieldEvent_GetFieldSysWork( event );
	wk   = FieldEvent_GetSpecialWork( event );

	switch( wk->seq ){
	case 0:
		EventCmd_FinishFieldMap( event );	// イベントコマンド：フィールドマッププロセス終了
		wk->seq++;
		break;

	case 1:
		EventSet( fsys, wk );
		wk->seq++;
		break;

	case 2:
		EventCmd_StartFieldMap( event );
		wk->seq++;
		break;

	case 3:
		sys_FreeMemoryEz( wk );
		return TRUE;
	}

	return FALSE;
}

//--------------------------------------------------------------------------------------------
/**
 * デモイベントセット
 *
 * @param	fsys	フィールドシステム
 * @param	cwk		呼び出しワーク
 *
 * @return	none
 */
//--------------------------------------------------------------------------------------------
static void EventSet( FIELDSYS_WORK * fsys, LD_CALL_WORK * cwk )
{
	LD_EVENT_WORK * ewk;

	ewk = sys_AllocMemoryLo( HEAPID_WORLD, sizeof(LD_EVENT_WORK) );
	ewk->call = cwk;
	ewk->seq  = 0;

	FieldEvent_Call( fsys->event, LegendDemoEvent, ewk );
}

//--------------------------------------------------------------------------------------------
/**
 * デモ管理イベント
 *
 * @param	event
 *
 * @return	処理状況
 */
//--------------------------------------------------------------------------------------------
static BOOL LegendDemoEvent( GMEVENT_CONTROL * event )
{
	FIELDSYS_WORK * fsys;
	LD_EVENT_WORK * ewk;
	LD_CALL_WORK * cwk;

	fsys = FieldEvent_GetFieldSysWork( event );
	ewk  = FieldEvent_GetSpecialWork( event );
	cwk  = ewk->call;

	switch( ewk->seq ){
	case 0:
//		OS_Printf( "color[0] = %d\n", fsys->glst_data->Color[0] );
		ewk->dat = LegendDemo_SetProc( fsys, &cwk->glst, cwk->mode, cwk->time, HEAPID_WORLD );
		ewk->seq++;
		break;

	case 1:
		if( !( FieldEvent_Cmd_WaitSubProcEnd(fsys) ) ) {
			ewk->seq++;
		}
		break;

	case 2:
		sys_FreeMemoryEz( ewk->dat );
		sys_FreeMemoryEz( ewk );
		return TRUE;
	}

	return FALSE;
}